package engine;
import java.util.Random;

public class Deck{
	private int nextCard = 0;
	private Card[] deck;
	
	public Deck(){
		deck = new Card[Card.getNrOfSuits() * Card.getNrOfRanks()];
		for (int j = 0; j < Card.getNrOfSuits(); j++) {
			for (int k = 0; k < Card.getNrOfRanks(); k++) {
				int nr =  j * Card.getNrOfRanks() + k;
				deck[nr] = new Card(j, k);
			}
		}

		shuffle(deck);
	}

	public Card giveCard() {
		if (nextCard < deck.length) {
			nextCard++;
			return deck[nextCard - 1];
		}
		return null;
	}

	private void swapCards(Card[] d, int a, int b) {
		Card temp = d[a];
		d[a] = d[b];
		d[b] = temp;
	}

	private void shuffle(Card[] d) {
		Random generator = new Random();
		for (int i = d.length - 1; i > 0; i--) {
			swapCards(d, i, generator.nextInt(i + 1));
		}
	}
}